package com.risen.project.config;

import com.risen.project.filter.RequestFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 过滤器配置 指定顺序 处于同一个过滤器链中 会按照注册顺序依次执行
 * 配置了多个过滤器 由于过滤规则相同 所以这些过滤器为一条过滤器链 filterChain.doFilter()是把request和response交给下一个过滤器 前提是他们的过滤规则相同
 */
@Configuration
public class FilterConfig {

    /**
     * 注册请求过滤器
     *
     * @return 请求过滤器包装的bean
     */
    @Bean
    public FilterRegistrationBean<RequestFilter> requestSourceFilterBean() {
        FilterRegistrationBean<RequestFilter> filterRegistrationBean = new FilterRegistrationBean<>();

        RequestFilter requestFilter = new RequestFilter();

        filterRegistrationBean.setFilter(requestFilter);
        filterRegistrationBean.addUrlPatterns("/project/projectSend");//配置过滤规则
        filterRegistrationBean.setName("requestFilter");//设置过滤器名称

        return filterRegistrationBean;
    }
}
